﻿jQuery.extend({
    //使用JSON提交到WS，并处理返回值【返回值为事先定义好格式的】
    ajaxJSON: function(u, args, suc, err, popError, popInfo, sync) {
        jQuery.ajax({
            async: !sync,
            type: "post",
            contentType: "application/json",
            url: u,
            data: args,
            dataType: "json",
            success: function(result) {
                if (popError && jQuery.isServiceError(result)) {
                    alert(result.Msg);
                    return;
                }
                suc(result);
                if (popInfo && jQuery.isServiceInfo(result)) {
                    alert(result.Msg);
                }
            },
            error: err
        })
    }
});
jQuery.extend({
    //判断对象是否为空以及字符串是否为空
    isNullOrEmpty: function(obj) {
        return obj?false:true;
    }
});
jQuery.extend({
    //JSON方式的AJAX，返回值是否包含错误信息【返回值为事先定义好格式的】
    isServiceError: function(result) {
        return (!jQuery.isNullOrEmpty(result.Msg) && result.IsError)
    }
});
jQuery.extend({
    //JSON方式的AJAX，返回值是否包含提示信息【返回值为事先定义好格式的】
    isServiceInfo: function(result) {
        return (!jQuery.isNullOrEmpty(result.Msg) && !result.IsError)
    }
});
jQuery.extend({
    //冻结页面执行
    freeze: function(time) {
        var date = new Date();
        var cur = null;
        do {
            cur = new Date();
        } while (cur - date < time);
    }
});
jQuery.extend({
    //由字符串返回JSON对象
    evalJSON: function(strJson) {
        return eval("(" + strJson + ")");
    }
});
jQuery.extend({
    //将JSON对象转换成字符串
    toJSON: function(object) {
        var type = typeof object;
        if ('object' == type) {
            if (Array == object.constructor)
                type = 'array';
            else if (RegExp == object.constructor)
                type = 'regexp';
            else
                type = 'object';
        }
        switch (type) {
            case 'undefined':
            case 'unknown':
                return;
            case 'function':
            case 'boolean':
            case 'regexp':
                return object.toString();
            case 'number':
                return isFinite(object) ? object.toString() : 'null';
            case 'string':
                return '"' + object.replace(/(\\|\")/g, "\\$1").replace(/\n|\r|\t/g,
                function() {
                    var a = arguments[0];
                    return (a == '\n') ? '\\n' :
                       (a == '\r') ? '\\r' :
                       (a == '\t') ? '\\t' : ""
                }) + '"';
                break;
            case 'object':
                if (object === null) return 'null';
                var results = [];
                for (var property in object) {
                    var value = jQuery.toJSON(object[property]);
                    if (value !== undefined)
                        results.push(jQuery.toJSON(property) + ':' + value);
                }
                return '{' + results.join(',') + '}';
                break;
            case 'array':
                var results = [];
                for (var i = 0; i < object.length; i++) {
                    var value = jQuery.toJSON(object[i]);
                    if (value !== undefined) results.push(value);
                }
                return '[' + results.join(',') + ']';
                break;
        }
    }
});